﻿#include "IFBlock.h"
#include "QBlock.h"
namespace CVV
{
namespace FlowChart
{

IFBlock::IFBlock(QString &name, QRect &position, double zoom, bool isExpand, QMargins margins)
{
    setZoom(zoom);
    setPostion(position);
    setMargin(margins);
    setName(name);
    m_isExpand = isExpand;
}

IFBlock::~IFBlock()
{

}

void IFBlock::paint(QPainter *canvas)
{
    double y=postion().y();
    double x=postion().x();
    double width=postion().width();
    double height=postion().height();
    double hcenter = x + width / 2;
    double a = m_aScale * zoom();
    double b = m_bScale * a;
    double bottom = y + height;

    SetNewPen(2)
    canvas->drawLine(QLineF(hcenter, y-0.5, hcenter, y + m_lineScale * zoom()));
    IFBlock::drawBottomArrow(canvas, QPointF(hcenter, y + m_lineScale * zoom()),
                                QSize(static_cast<int>(6 * zoom()), static_cast<int>(12 * zoom())));

    QPointF par[3][4];
    for(int i = 0 ; i < 3; i++)
    {
        par[i][0] = QPointF(hcenter - b/2 + 3*i, y + m_lineScale * zoom() + a/2 + 3*i);
        par[i][1] = QPointF(hcenter + 3*i      , y + m_lineScale * zoom() + 3*i);
        par[i][2] = QPointF(hcenter + b/2 + 3*i, y + m_lineScale * zoom() + a/2 + 3*i);
        par[i][3] = QPointF(hcenter + 3*i      , y + m_lineScale * zoom() + a   + 3*i);
    }

    QColor bgColor = m_backgroundColor;
    if(!m_isExpand)
    {
        bgColor = m_collapseBgColor;
    }
    QBrush brush = canvas->brush();
    canvas->setBrush(bgColor);
    if(m_isExpand)
    {
        canvas->drawPolygon(par[0], 4);
    }
    else{
        for(int di = 2; di >= 0; di--)
        {
            canvas->drawPolygon(par[di], 4);
        }
    }
    canvas->setBrush(brush);

    UseOldPen

    QRectF textRect(hcenter - b/2 + a/4 + 20, y + m_lineScale * zoom()+4 + a/8, b - a/2 - 40, a - 8 - a/4);
    canvas->drawText(textRect, Qt::AlignCenter | Qt::TextWrapAnywhere, QString("%1").arg(name()));
    if(m_isExpand)
    {
        QBlock *left = getBranchItem().at(0);
        Q_ASSERT_X(left != nullptr, "QBlock::paint()" ,"item(0) == 0. i.e. left branch of IF is nul.");
        QBlock *right = getBranchItem().at(1);
        Q_ASSERT_X(right != nullptr, "QBlock::paint()" ,"item(1) == 0. i.e. right branch of IF is nul.");
        // Left line
        QPointF line[3];
        line[0] = QPointF(hcenter - b / 2, y + m_lineScale * zoom() + a / 2);
        line[1] = QPointF(left->m_dX + left->m_dWidth / 2, y + m_lineScale * zoom() + a / 2);
        line[2] = QPointF(left->m_dX + left->m_dWidth / 2, left->m_dY);

        SetNewPen(2)
        canvas->drawPolyline(line, 3);

        // Right line
        line[0] = QPointF(hcenter + b/2, y + m_lineScale * zoom() + a/2);
        line[1] = QPointF(right->m_dX + right->m_dWidth / 2, y + m_lineScale * zoom() + a/2);
        line[2] = QPointF(right->m_dX + right->m_dWidth / 2, right->m_dY);
        canvas->drawPolyline(line, 3);
        // connect
        QPointF collector[4];
        collector[0] = QPointF(left->m_dX + left->m_dWidth / 2, left->m_dY + left->m_dHeight);
        collector[1] = QPointF(left->m_dX + left->m_dWidth / 2, bottom - 8*zoom());
        collector[2] = QPointF(right->m_dX + right->m_dWidth / 2, bottom - 8*zoom());
        collector[3] = QPointF(right->m_dX + right->m_dWidth / 2, right->m_dY + right->m_dHeight);
        canvas->drawPolyline(collector, 4);
        canvas->drawLine(QLineF(hcenter, bottom-8*zoom(), hcenter, bottom+0.5));
        UseOldPen

        canvas->setPen(Qt::white);
        canvas->drawText(QPointF(hcenter - b/2 - 24*zoom(), y + 12 * zoom() + a/2), QObject::tr("Yes"));
        canvas->drawText(QPointF(hcenter + b/2 +5*zoom(), y + 12 * zoom() + a/2), QObject::tr("No"));
    }
    else
    {
        UseOldPen
        canvas->setPen(Qt::white);
        canvas->drawText(QPointF(hcenter + b/3, y + 12 * zoom() + a/3), QObject::tr("Fold"));
    }
}

QString IFBlock::getBlockDom()
{
    return  "<algorithm><branch><if cond=\"x &gt; 0\" id=\"%1\" category=\"%2\" code=\"%3\" title=\"%4\" isPlugin=\"true\"><branch /><branch /></if></branch></algorithm>";
}
}
}
